package navstatus

import (
	"time"

	"github.com/BertoldVdb/go-ais"
)

const (
	//UsingEngine 发动机使用中/正在使用引擎的方式
	UsingEngine uint8 = 0

	//AtAnchor 下锚/锚泊
	AtAnchor uint8 = 1

	//NotUnderCommand 失控
	NotUnderCommand uint8 = 2

	//Restricted 操作受限/受限制的机动性
	Restricted uint8 = 3

	//Constrained 吃水受限
	Constrained uint8 = 4

	//Moored 系泊
	Moored uint8 = 5

	//Aground 搁浅
	Aground uint8 = 6

	//Fishing 从事捕鱼
	Fishing uint8 = 7

	//Sailing 正在航行中
	Sailing uint8 = 8

	//SartMobOrEpirb AIS-SART处于活动状态
	SartMobOrEpirb uint8 = 14

	//Undefined 未定义（默认）
	Undefined uint8 = 15

	//Reserved9 Reserved For Future Use 9
	Reserved9 uint8 = 9

	//Reserved10 Reserved For Future Use 10
	Reserved10 uint8 = 10

	//VesselTowing Power Driven Vessel Towing Astern 船尾推作业
	VesselTowing uint8 = 11

	//PushingOrTowing Power Driven Vessel Pushing Ahead Or Towing Alongside 推或旁拖
	PushingOrTowing uint8 = 12

	//Reserved13 Reserved For Future Use 13
	Reserved13 uint8 = 13
)

// GetReportDuration 获取 ais 位置报告间隔
// A类船载移动设备的报告间隔2
//   - 锚泊或系泊且移动速度不超过3节的船舶 3 min(1)
//   - 锚泊或系泊且移动速度超过3节的船舶 10 s (1)
//   - 0-14节的船舶 10 s(1)
//   - 0-14节且改变航向的船舶 3 1/3 s(1)
//   - 14-23节的船舶 6 s(1)
//   - 14-23节且改变航向的船舶 2 s
//   - > 23节的船舶 2 s
//   - > 23节且改变航向的船舶 2 s
func GetReportDuration(r *ais.PositionReport) time.Duration {
	navStatus := r.NavigationalStatus
	if navStatus == AtAnchor || navStatus == Moored {
		if r.Sog < 3 {
			return 3 * time.Minute //3 min
		}
		return 10 * time.Second
	}
	//是否正在转向
	var turning = r.RateOfTurn > 0
	if r.Sog < 14 {
		if turning {
			return 3*time.Second + 333*time.Millisecond //3.3;
		}
		return 10 * time.Second
	}
	if r.Sog < 23 && !turning {
		return 6 * time.Second
	}
	return 2 * time.Second
}
